﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krilib.Control;
using System.IO;
using System.Drawing.Imaging;

namespace Wassrile.Controls
{
    public partial class WInput : UserControl, IInputMethod
    {
        public WInput()
        {
            InitializeComponent();
        }

        public MethodInvoker CancelCallback { get; set; }

        public void Cancelled()
        {
            if (palletdlg != null)
            {
                palletdlg.Close();
                palletdlg.Dispose();
                palletdlg = null;
            }
        }

        public void Clear()
        {
            MainText.Clear();
            if (palletdlg != null)
            {
                palletdlg.Close();
            }
            linkedPicturePath.Text = "なし";
            linkedPicturePath.Enabled = false;
            linkPicture = null;
            postToCh = null;
        }

        public object ExtraData
        {
            get
            {
                if (String.IsNullOrEmpty(linkPicture) && String.IsNullOrEmpty(postToCh))
                    return null;
                else
                    return new WExtraData(linkPicture, postToCh);
            }
            set
            {
                if (value == null || !(value is WExtraData) )
                    return;
            }
        }

        public void FocusToInput()
        {
            MainText.Focus();
        }

        public string IdentifyString
        {
            get { return Define.InputMethodId; }
        }

        public string InputText
        {
            get
            {
                return MainText.Text;
            }
            set
            {
                MainText.Text = value;
            }
        }

        public MethodInvoker PostCallback { get; set; }

        private void PostButton_Click(object sender, EventArgs e)
        {
            PostCallback.Invoke();
        }

        private void ColoringBar_Click(object sender, EventArgs e)
        {
            ColorBar.Visible = ColoringBar.Checked;
        }

        Dialogs.PictPalletDlg palletdlg = null;
        private void PictoPallet_Click(object sender, EventArgs e)
        {
            if (palletdlg != null)
            {
                palletdlg.Close();
                palletdlg = null;
                PictoPallet.Checked = false;
                return;
            }
            palletdlg = new Wassrile.Dialogs.PictPalletDlg();
            if (Core.Config.Behavior.PictPallet.saveSize)
                palletdlg.Size = Core.Config.State.PalletSize;
            palletdlg.Location =
                Core.Config.Behavior.PictPallet.saveLocation ?
                Core.Config.State.PalletLoc :
                MainStrip.PointToScreen(new Point());
            palletdlg.Top -= palletdlg.Size.Height;
            palletdlg.PalletClicked += new Wassrile.Dialogs.Pictogram.PictogramSet.ClickPallet(palletdlg_PalletClicked);
            palletdlg.Disposed += new EventHandler(palletdlg_Disposed);
            palletdlg.Show();
        }

        void palletdlg_Disposed(object sender, EventArgs e)
        {
            palletdlg = null;
            PictoPallet.Checked = false;
        }

        void palletdlg_PalletClicked(object sender, Wassrile.Dialogs.Pictogram.PictogramSet.StringEventArgs e)
        {
            MainText.SelectedText = "{emoji:" + e.code + "}";
        }

        private void SetColor_Click(object sender, EventArgs e)
        {
            if (MainText.SelectedText == String.Empty)
            {
                MessageBox.Show(
                    "色付けしたい文字を選択してからクリックしてください",
                    "Wassr文字着色",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            MainText.SelectedText = "{color:" + ((ToolStripButton)sender).Name + ":" + MainText.SelectedText + "}";
        }

        public class WExtraData
        {
            public string LinkPicturePath = null;
            public string ChannelId = null;
            public WExtraData() { }
            public WExtraData(string lpict, string ch)
            {
                LinkPicturePath = lpict;
                ChannelId = ch;
            }
        }

        private void MainText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Control)
            {
                e.Handled = true;
                PostCallback();
            }
        }

        string linkPicture = null;
        private void LinkPicture_ButtonClick(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Filter = "アップロード可能な画像ファイル|*.jpg;*.jpeg;*.png|すべてのファイル|*.*";
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    linkedPicturePath.Text = "添付(クリックで解除):" + ofd.FileName;
                    linkedPicturePath.Enabled = true;
                    linkPicture = ofd.FileName;
                }
            }
        }

        private void linkedPicturePath_Click(object sender, EventArgs e)
        {
            if (linkPicture != null)
            {
                if (MessageBox.Show("添付を解除しますか？", "画像の添付",
                     MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    linkPicture = null;
                    linkedPicturePath.Text = "なし";
                    linkedPicturePath.Enabled = false;
                }
            }
        }

        private void CaptureDesktop_Click(object sender, EventArgs e)
        {
            using(var cf = new CaptureFrame())
            {
                if (cf.ShowDialog() == DialogResult.OK)
                {
                    Application.DoEvents();
                    Rectangle target = cf.Target;
                    using (Bitmap bmp = new Bitmap(target.Width, target.Height, PixelFormat.Format32bppArgb))
                    {
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            g.CopyFromScreen(target.X, target.Y, 0, 0,
                              target.Size, CopyPixelOperation.SourceCopy);
                        }
                        using (var sfd = new SaveFileDialog())
                        {
                            try
                            {
                                sfd.Filter = "PNG画像|*.png|JPEG画像|*.jpg|すべてのファイル|*.*";
                                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                                if (sfd.ShowDialog() == DialogResult.OK)
                                {
                                    bmp.Save(sfd.FileName);
                                }
                                linkedPicturePath.Text = "添付(クリックで解除):" + sfd.FileName;
                                linkedPicturePath.Enabled = true;
                                linkPicture = sfd.FileName;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("エラーが発生しました:" + Environment.NewLine + ex.Message);
                            }
                        }
                    }
                }
            }
        }

        string postToCh = null;
        private void ChSelector_Click(object sender, EventArgs e)
        {

        }

        public void SetChannel(string id)
        {

        }

    }
}
